Functional (C )
   HOME

TheInfoList



OR:

In the context of the programming language
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, functional refers to a
header file Many programming languages and other computer files have a directive, often called include (sometimes copy or import), that causes the contents of the specified file to be inserted into the original file. These included files are called copybooks ...
that is part of the
C++ Standard Library The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard.ISO/IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C §7'' Starting from the original ANSI C standard, it wa ...
and provides a set of predefined class templates for
function object In computer programming, a function object is a construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax (a function parameter that can also be a function). Function objects are often ca ...
s, including operations for arithmetic, comparisons, and logic. Instances of these class templates are
C++ classes A class in C++ is a user-defined type or data structure declared with keyword class that has data and functions (also called member variables and member functions) as its members whose access is governed by the three access specifiers ''private'' ...
that define a
function call operator This is a list of operators in the C and C++ programming languages. All the operators listed exist in C++; the column "Included in C", states whether an operator is also present in C. Note that C does not support operator overloading. When not ...
, and the instances of these classes can be called as if they were functions. It is possible to perform very sophisticated operations without writing a new function object, simply by combining predefined function objects and function object adaptors. The class template std::function provided by
C++11 C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by ...
is a general-purpose polymorphic function wrapper. Instances of std::function can store, copy, and invoke any callable target—functions, lambda expressions (expressions defining
anonymous function In computer programming, an anonymous function (function literal, lambda abstraction, lambda function, lambda expression or block) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to ...
s), bind expressions (instances of function adapters that transform functions to other functions of smaller
arity Arity () is the number of arguments or operands taken by a function, operation or relation in logic, mathematics, and computer science. In mathematics, arity may also be named ''rank'', but this word can have many other meanings in mathematics. In ...
by providing values for some of the arguments), or other function objects. The
algorithms In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing c ...
provided by the C++ Standard Library do not require function objects of more than two arguments. Function objects that return Boolean values are an important special case. A
unary function A unary function is a function that takes one argument. A unary operator belongs to a subset of unary functions, in that its range coincides with its domain. In contrast, a unary function's domain may or may not coincide with its range. Exampl ...
whose return type is is called a ''predicate'', and a binary function whose return type is is called a ''binary predicate''.


Adaptable function objects

In general, a function object has restrictions on the type of its argument. The type restrictions need not be simple, though: may be overloaded or may be a member template. Similarly, there need be no way for a program to determine what those restrictions are. An adaptable function object, however, does specify what the argument and return types are, and provides nested s so that those types can be named and used in programs. If a type is a model of an adaptable generator, then it must define . Similarly, if is a model of the adaptable unary function, it must define and , and if is a model of the adaptable binary function, it must define , , and . The C++ Standard Library provides base classes and to simplify the definition of adaptable unary functions and adaptable binary functions. Adaptable function objects are important, because they can be used by function object adaptors: function objects that transform or manipulate other function objects. The C++ Standard Library provides many different function object adaptors, including (that returns the logical complement of the value returned by a particular adaptable predicate), and and , which perform composition of function object.


Predefined function objects

The C++ Standard Library includes in the
header file Many programming languages and other computer files have a directive, often called include (sometimes copy or import), that causes the contents of the specified file to be inserted into the original file. These included files are called copybooks ...
functional many different predefined function objects, including arithmetic operations (, , , , , and ), comparisons (, , , , , and ), and logical operations (, , and ).


Examples

Function wrappers can be used to make calls to ordinary functions or to functions objects created by lambda expressions. #include #include /* Define a template function */ template void PrintValue(T value) int main(void) Function wrappers also can be used to access
member variable In object-oriented programming, a member variable (sometimes called a member field) is a variable that is associated with a specific object, and accessible for all its methods (''member functions''). In class-based programming languages, these are ...
s and
member function A method in object-oriented programming (OOP) is a procedure associated with a message and an object. An object consists of ''state data'' and ''behavior''; these compose an ''interface'', which specifies how the object may be utilized by any of ...
s of classes. #include #include template class CAnyData ; int main()


References

{{reflist


External links


C++ reference for standard function objects
C++ C++ Standard Library Articles with example C++ code